package BuddyTemplate;



/*
 * QuestionFrame.java
 *
 * Created on February 15, 2008, 11:37 AM
 */

/**
 *
 * @author  Peter
 */

import java.util.*;
import java.io.*; 
import javax.swing.*;

public class QuestionFrame extends javax.swing.JFrame {
    
    QuestionInterface QuestionCollection;
    AdaptiveEngine AE;
    
    /*Creating the constructors which pass the nextQuestion information, score, 
     *and current questtion information
     */
    
    public QuestionFrame(int theScore, QuestionInterface TheQuestionCollection, Question temp) {
        SaveGame.score = theScore;

        initComponents();
        

        QuestionCollection = TheQuestionCollection;
                
        Question<String, String> temp1;
        
        try{
            temp1 = (Question<String, String>)temp;
            
            QuestionArea.setText((String)temp1.getQuestion());
            
            if (temp.choices.size() >= 1){
                    AnsAreaA.setText((String)temp.choices.get(0)); 
            } else {
                    AnsAreaA.setVisible(false);
                    AnsA.setEnabled(false);
            }
            
            if (temp.choices.size() >= 2){
                    AnsAreaB.setText((String)temp.choices.get(1)); 
            } else {
                    AnsAreaB.setVisible(false);
                    AnsB.setEnabled(false);
            } 
            
            if (temp.choices.size() >= 3){
                    AnsAreaC.setText((String)temp.choices.get(2)); 
            } else {
                    AnsAreaC.setVisible(false);
                    AnsC.setEnabled(false);
            }
          
            if (temp.choices.size() >= 4){
                    AnsAreaD.setText((String)temp.choices.get(3)); 
            } else {
                    AnsAreaD.setVisible(false);
                    AnsD.setEnabled(false);
            } 
        }
        catch (Exception e){
            

            System.out.println("Casting Error");

            if (temp.choices.size() >= 1){
                    AnsAreaA.setText(temp.choices.get(0).toString()); 
            } else {
                    AnsAreaA.setVisible(false);
                    AnsA.setEnabled(false);
            }
            
            if (temp.choices.size() >= 2){
                    AnsAreaB.setText(temp.choices.get(1).toString()); 
            } else {
                    AnsAreaB.setVisible(false);
                    AnsB.setEnabled(false);
            } 
            
            if (temp.choices.size() >= 3){
                    AnsAreaC.setText(temp.choices.get(2).toString()); 
            } else {
                    AnsAreaC.setVisible(false);
                    AnsC.setEnabled(false);
            }
          
            if (temp.choices.size() >= 4){
                    AnsAreaD.setText(temp.choices.get(3).toString()); 
            } else {
                    AnsAreaD.setVisible(false);
                    AnsD.setEnabled(false);
            } 
            
            QuestionArea.setText(temp.getQuestion().toString());
            
            
 
        }
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTextArea6 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        QuestionArea = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        AnsAreaA = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        AnsAreaB = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        AnsAreaC = new javax.swing.JTextArea();
        jScrollPane6 = new javax.swing.JScrollPane();
        AnsAreaD = new javax.swing.JTextArea();
        AnsA = new javax.swing.JCheckBox();
        AnsB = new javax.swing.JCheckBox();
        AnsC = new javax.swing.JCheckBox();
        AnsD = new javax.swing.JCheckBox();
        Return = new javax.swing.JButton();
        NextQ = new javax.swing.JButton();
        AnsCheck = new javax.swing.JLabel();

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);

        jTextArea6.setColumns(20);
        jTextArea6.setRows(5);
        jScrollPane5.setViewportView(jTextArea6);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Study Buddy");
        setBackground(java.awt.Color.gray);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Question", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));

        QuestionArea.setColumns(20);
        QuestionArea.setRows(5);
        jScrollPane3.setViewportView(QuestionArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Answer", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));

        AnsAreaA.setColumns(20);
        AnsAreaA.setRows(5);
        AnsAreaA.setText("asdf");
        jScrollPane1.setViewportView(AnsAreaA);

        AnsAreaB.setColumns(20);
        AnsAreaB.setRows(5);
        jScrollPane2.setViewportView(AnsAreaB);

        AnsAreaC.setColumns(20);
        AnsAreaC.setRows(5);
        jScrollPane4.setViewportView(AnsAreaC);

        AnsAreaD.setColumns(20);
        AnsAreaD.setRows(5);
        jScrollPane6.setViewportView(AnsAreaD);

        AnsA.setText("A");

        AnsB.setText("B");

        AnsC.setText("C");

        AnsD.setText("D");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(AnsA)
                            .addComponent(AnsB)
                            .addComponent(AnsC))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 875, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 875, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 875, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(AnsD)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 875, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnsA))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnsB))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AnsC)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnsD))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        Return.setFont(new java.awt.Font("Tahoma", 1, 14));
        Return.setText("Main Menu");
        Return.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReturnActionPerformed(evt);
            }
        });

        NextQ.setFont(new java.awt.Font("Tahoma", 1, 14));
        NextQ.setText("Next Question");
        NextQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextQActionPerformed(evt);
            }
        });

        AnsCheck.setFont(new java.awt.Font("Tahoma", 0, 18));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(Return, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(109, 109, 109)
                        .addComponent(AnsCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 178, Short.MAX_VALUE)
                        .addComponent(NextQ, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Return, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnsCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NextQ, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /*This function will check whether the user have answer the question
     * correctly, records the score, and move on to the next question.
     */
    private void NextQActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextQActionPerformed
        System.out.println("early Score is: " + SaveGame.score);
        ArrayList<Boolean> ansprovided = new ArrayList<Boolean>();
        int Answer = 0;
        
        
        //This will get the selection from the user
        ansprovided.add(0, AnsA.isSelected());
        ansprovided.add(1, AnsB.isSelected());
        ansprovided.add(2, AnsC.isSelected());
        ansprovided.add(3, AnsD.isSelected());
        
        //Checking the score of the user
        Answer = QuestionCollection.CheckAnswer(ansprovided);
        
        //Checking if this is the users first attempt to solve the question
        if (checker == true)
        {
            SaveGame.score = SaveGame.score + Answer;
            checker = false;
        }
        
        /*If the users get the answer correct, it will check if the current
         * question is the last question if not, it will select the proper frame
         * to use for the next question.
         */
        if (Answer == 4)
        {
            //check if the current question is the last question
            if (QuestionCollection.isFinal())
            {
                JOptionPane.showMessageDialog(null, "No more questions are avaiable.");
                FinalScoreFrame FSF = new FinalScoreFrame(SaveGame.score);
                FSF.setVisible(true);
                this.setVisible(false);
                this.dispose();
            }
            
            /*If it's not the last question, it wil figure out which question it is
             * and will select the proper frame for the next question
             */
            
            else
            {    
                Question thenextquestion = QuestionCollection.nextQuestion();
                if(thenextquestion.choices.get(0) instanceof String && thenextquestion.getQuestion() instanceof String){
                    QuestionFrame nextQuestion = new QuestionFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else if(thenextquestion.choices.get(0) instanceof ImageIcon && thenextquestion.getQuestion() instanceof String){
                    PictureFrame nextQuestion = new PictureFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else if(thenextquestion.choices.get(0) instanceof String && thenextquestion.getQuestion() instanceof ImageIcon){
                    PictureChoiceFrame nextQuestion = new PictureChoiceFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else{
                    QuestionFrame nextQuestion = new QuestionFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                this.setVisible(false);
                this.dispose();
            }
        }
        
        //If the answer is incorrent, the user will get to try again.
        else
        {
            AnsCheck.setText("Incorrect, please try again.");
        }
              
    }//GEN-LAST:event_NextQActionPerformed

    private void ReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReturnActionPerformed
        this.setVisible(false);
        dispose();
        /*BuddyLoader BL = new BuddyLoader();
        BL.setVisible(true);
        this.setVisible(false);
        dispose();
        */
}//GEN-LAST:event_ReturnActionPerformed
    
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox AnsA;
    private javax.swing.JTextArea AnsAreaA;
    private javax.swing.JTextArea AnsAreaB;
    private javax.swing.JTextArea AnsAreaC;
    private javax.swing.JTextArea AnsAreaD;
    private javax.swing.JCheckBox AnsB;
    private javax.swing.JCheckBox AnsC;
    private javax.swing.JLabel AnsCheck;
    private javax.swing.JCheckBox AnsD;
    private javax.swing.JButton NextQ;
    private javax.swing.JTextArea QuestionArea;
    private javax.swing.JButton Return;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextArea6;
    // End of variables declaration//GEN-END:variables
    public boolean checker = true;
}
